// SimNet is a Second Life(tm) sim mangement tool that was designed for Fermi Sandbox
// Copyright (C) 2011 Arthur Fermi
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To receive a copy of this license please visit http://www.gnu.org/licenses/

// SimNet - Configuration and Verification



// Please set the following items to make your toolbar work.
// After you are done setting these, you should change this script with
// the following pemisions
// [X] Copy  [  ] Modify  [  ] Transfer
// 
// The information here could be used to get around your SimNet system.
// so it is important that you limit anyone from editing.  If you need to
// pass a version to another staff member who you want to give it out you
// can change the permissions back, and give it to them, but make sure they 
// set the permissions before it is given out.
//

// Set the URL to your web site, this will be used by multiple components
string  gstrURL = "http://yourwebsite/simnet/";

// Set the name of the person or person who will be managing your SimNet installation,
// this is purely text so it can be more than one person.
// Examples
// One Person
//     "Arthur Fermi"
//
// Two People
//     "Arthur Fermi or Fermi Shepherd"
string  gintSimNetManger = "Manager Name";  

// Set the name of the home sim where the updating device will be located.
// Users are can only update in the home sim.
string  gstrSimNetHome = "My Sim"; 

// Set the channel for your update server, you will need to set the channel to be the same in the update server.
integer gintUpdateCN = -123456;

// Set the version of the toolbar, it should match the version that is on the web site.
string  gstrToolbarVer = "1.01";

// Do not change anything below here or you will likely break something
string  gstrHTTP;

default
{
    state_entry()
    {
        // Try to attach if rezed on the ground
        llRequestPermissions(llGetOwner(), PERMISSION_ATTACH);

        llOwnerSay("SimNet toolbar verification tool");
        
        // Verify that this user has access to the toolbar
        llOwnerSay("Verifying toolbar permissions ... please wait.");
        gstrHTTP = llHTTPRequest(gstrURL + "simnet_verify.php?VerifyWho=" + llEscapeURL(llKey2Name(llGetOwner())) ,[HTTP_METHOD, "POST"], "");    
    }
    
    link_message(integer sender_num, integer num, string str, key id)
    {
        // Listen for URL requests
        if (num == 333)
        {
            llMessageLinked(LINK_SET, 334, gstrURL, NULL_KEY);
        }
        
        if (num == 1000)
        {
            
            if (str == "Icons")
            {
                // Rotate to 90 degrees
                vector eul = <0,0,90>; //90 degrees around the z-axis, in Euler form
                eul *= DEG_TO_RAD; //convert to radians
                rotation quat = llEuler2Rot(eul); //convert to quaternion
                llSetRot(quat); //rotate the object             
            }
            
            else if (str == "Icons+Text")
            {
                // Set the rotation to 0 degress
                vector eul = <0,0,0>; //0 degrees around the z-axis, in Euler form
                eul *= DEG_TO_RAD; //convert to radians
                rotation quat = llEuler2Rot(eul); //convert to quaternion
                llSetRot(quat); //rotate the object            
            }
        }
    }
    
    http_response(key request_id, integer status, list metadata, string body)
    {
        list    lstTempList;
        string  strTempDesciption;
        string  strComm1;
        string  strVerify;
        string  strVersion;
        
        if (request_id == gstrHTTP)
        {
            // Convert web page to a list
            lstTempList = llParseString2List(body, ["^"], []);
            
            // Set Verfiy Result
            strVerify = (string)llList2List(lstTempList,1,1);
            if (strVerify == "1")
            {
                // get Variable to build the string
                strComm1 =  llStringTrim((string)llList2List(lstTempList,0,0), STRING_TRIM);            
                strVersion = llStringTrim((string)llList2List(lstTempList,4,4), STRING_TRIM);
                
                // Build Description
                llSetObjectDesc(strVersion + "^" + strComm1 + "^" + strVersion);
                
                llOwnerSay("You are authorized to use this toolbar!\n");
                llOwnerSay("Checking for updates ... please wait");
                llSetObjectName(llKey2Name(llGetOwner()) + "'s manager toolbar");

                // Version Check
                if (strVersion != gstrToolbarVer)
                {
                    // Check location, update will only work in the home sim.
                    if ( llGetRegionName() == gstrSimNetHome)
                    {
                        llOwnerSay("Your toolbar is out of date, requesting an update.  If you have any problems please contact " + gintSimNetManger);
                        // Get update package
                        llRegionSay(gintUpdateCN, llKey2Name(llGetOwner()) + "^" + (string)llGetOwner() + "^SimNet Toolbar Update");
                    }
                    else
                    {
                        llOwnerSay("I can only auto update in " + gstrSimNetHome + ", please go to " + gstrSimNetHome + " sim and remove your toolbar and reattach it.");
                    }
                }
                else
                {
                    llOwnerSay("Your toolbar is up to date.");
                    llOwnerSay("You SimNet toolbar is ready for use.");
                }                 
            }
            else
            {
                llOwnerSay("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n  This HUD is not authorized for use  \n  If you feel this is in error please \n  contact Arthur Fermi. \n  This HUD will automatically detach\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
                
                // Detatch from Avatar
                llDetachFromAvatar();
            }
        }
    }
    
    on_rez(integer start_param)
    {
        llResetScript();
    }
    
}


